package com.example.denglu.util;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBUtil {
    private static final String URL = "jdbc:mysql://rm-uf6l6v5536oa850czpo.mysql.rds.aliyuncs.com:3306/login?useSSL=false";
    private static final String USERNAME = "DNjokey";
    private static final String PASSWORD = "Zhaoruihan041210";

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws Exception {
        System.out.println("尝试连接数据库");
        try {
            Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            System.out.println("数据库连接成功");
            return conn;
        } catch (Exception e) {
            System.out.println("数据库连接失败：" + e.getMessage());
            throw e;
        }
    }

    public static void main(String[] args) {
        try {
            Connection conn = getConnection();
            System.out.println("数据库连接成功！");
            conn.close();
        } catch (Exception e) {
            System.out.println("数据库连接失败：" + e.getMessage());
            e.printStackTrace();
        }
    }
} 